package com.qiyang.contoller;

import com.qiyang.entity.UserVo;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import javax.validation.Valid;
import java.util.List;

/**
 * Created on 2020/12/11.
 *
 * @author qiyang.
 * 描述：
 */
@Controller
@RequestMapping("exception")
public class ExceptionController {

    /**
     * 演示全局异常处理
     * @return
     */
    @GetMapping("test")
    public String test(@RequestParam("name")String name) {
        System.out.println("演示全局异常处理");
        return "show";
    }

    /**
     * 异常处理方法
     * 注意，写在这里，只能处理本控制器异常
     * @param ex
     * @return
     */
    @ExceptionHandler(Exception.class)
    public ModelAndView handleException(Exception ex){
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("error");
        modelAndView.addObject("ex", ex);
        System.out.println("打印错误信息" + ex.getMessage());
        return modelAndView;
    }
}
